package com.example.demo.service;

import com.example.demo.enums.BookStatusEnum;
import com.example.demo.mapper.BookInfoMapper;
import com.example.demo.modal.BookInfo;
import com.example.demo.modal.PageRequest;
import com.example.demo.modal.PageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class BookService {
    @Autowired
    private BookInfoMapper bookInfoMapper;


    public void addBook(BookInfo bookInfo) {
        bookInfoMapper.insertBook(bookInfo);
    }

    public PageResponse<BookInfo> getListByPage(PageRequest pageRequest) {
        Integer count = bookInfoMapper.count();
        List<BookInfo> bookInfos = bookInfoMapper.queryBookListByPage(pageRequest.getOffset(), pageRequest.getPageSize());
        for (BookInfo bookInfo : bookInfos) {
            bookInfo.setStatusCN(BookStatusEnum.getStatusByCode(bookInfo.getStatus()).getDesc());
        }
        return new PageResponse<BookInfo>(count,bookInfos,pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookInfoMapper.queryBookById(bookId);
    }

    public Integer upadateBook(BookInfo bookInfo) {
        return bookInfoMapper.updateBook(bookInfo);
    }

    public void batchDeleteBook(List<Integer> ids) {
        bookInfoMapper.batchDeleteBook(ids);
    }
}
